package com.tanhua.dubbo.server.api;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.mapper.OptionMapper;

import com.tanhua.mapper.QuestionSoulMapper;
import com.tanhua.mapper.ReportMapper;
import com.tanhua.pojo.Option;
import com.tanhua.pojo.QuestionSoul;
import com.tanhua.pojo.Report;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service(version = "1.0.0")
public class SubmitQuestionnaireApiImpl implements SubmitQuestionnaireApi{

    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private QuestionSoulMapper questionSoulMapper;


    @Autowired
    private OptionMapper optionMapper;

    @Override
    public Integer slelctScore(String questionId, String optionId) {

        QueryWrapper<Option> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("question_id",Integer.parseInt(questionId)).
                eq("option_id",Integer.parseInt(optionId));

        Option option = optionMapper.selectOne(queryWrapper);

        return option.getScore();
    }


    @Override
    public void insertQuestionnaire(Report report) {

        reportMapper.insert(report);

    }

    @Override
    public Integer selectQuestionStar(Integer questionId) {
        QueryWrapper<QuestionSoul> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("question_id",questionId);

        QuestionSoul questionSoul = questionSoulMapper.selectOne(queryWrapper);

        return questionSoul.getQuestionStar();
    }

    @Override
    public Integer getReportId(Integer userId,Integer star) {
        QueryWrapper<Report> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper .eq("star",star)
                .orderBy(true,false,"create_time");

        List<Report> reports = reportMapper.selectList(queryWrapper);

        System.out.println(reports.size());

        Report report = reports.get(0);

        return report.getReportId().intValue();
    }
}